package com.doculibre.constellio.connector.twitter;

import java.io.File;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

import org.apache.commons.io.FileUtils;
import com.google.enterprise.connector.spi.Connector;
import com.google.enterprise.connector.spi.ConnectorShutdownAware;
import com.google.enterprise.connector.spi.RepositoryException;
import com.google.enterprise.connector.spi.RepositoryLoginException;
import com.google.enterprise.connector.spi.Session;
import com.doculibre.constellio.connector.persistence.config.PersistenceContext;
import com.doculibre.constellio.connector.persistence.config.PersistenceContextConfig;

/**
 * 
 * Twitter Connector that can retrieve public Tweets from an account and feed Tweet Documents to the GSA
 * 
 * @author France Labs
 * 
 */
public class TwitterConnector implements Connector, ConnectorShutdownAware {


	private static final Logger LOG = Logger.getLogger(TwitterConnector.class
			.getName());
	
	private String googleConnectorWorkDir;
	private String twitterAccount;
	private PersistenceContext persistenceContext;
	private PersistenceContextConfig persistenceContextConfig;


	public void setGoogleConnectorWorkDir(String googleConnectorWorkDir) {
		System.out.println("Setting google connector work dir to "
				+ googleConnectorWorkDir);
		this.googleConnectorWorkDir = googleConnectorWorkDir;
	}

	public String getGoogleConnectorWorkDir() {
		return googleConnectorWorkDir;
	}

	public void setTwitterAccount(String twitterAccount) {
		this.twitterAccount = twitterAccount;
	}

	public String getTwitterAccount() {
		return twitterAccount;
	}

    /**
     * DO NOT USE! CONSTELLIO INTERNAL API. 
     */
    public PersistenceContext getPersistenceContext() {
    	return persistenceContext;
    }

	@Override
	public Session login() throws RepositoryLoginException, RepositoryException {
		if (persistenceContextConfig == null) {
			persistenceContextConfig = new PersistenceContextConfig(new File(googleConnectorWorkDir + File.separator + "derby-db"),"twitter-connector-derby");
			persistenceContext = new PersistenceContext(persistenceContextConfig);
		}
		return new TwitterSession(persistenceContext, twitterAccount);
	}

	@Override
	public void delete() throws RepositoryException {
		try {
			FileUtils.cleanDirectory(new File(googleConnectorWorkDir));
		} catch (IOException e) {
			throw new RepositoryException(e);
		}
	}

	@Override
	public synchronized void shutdown() throws RepositoryException {
		LOG.info("Shutting down Twitter connector");
	
		if (persistenceContext != null) {
			try {
				persistenceContext.close();
				this.persistenceContext = null;
			} catch (Exception e) {
				LOG.warning(e.getMessage());
			} 
		}
		if (persistenceContextConfig != null) {
			String urlPath = persistenceContextConfig.getConnectionUrlPath();
			// FIXME Derby - specific
			LOG.info(urlPath);
			try {
				DriverManager.getConnection("jdbc:derby:" + urlPath + ";shutdown=true");
			} catch (SQLException e) {
				LOG.info(e.getMessage());
			}
			this.persistenceContextConfig = null;
		}
	}



}
